#!/usr/bin/python

#Audio Tools, a module and set of tools for manipulating audio data
#Copyright (C) 2007  Brian Langenberger

#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


import optparse,os.path
import audiotools

if (__name__ == '__main__'):
    parser = optparse.OptionParser(
        "%prog [options] <track 1> [track 2] ...",
        version="Python Audio Tools %s" % (audiotools.VERSION))
    parser.add_option("-n","--no-metadata",
                      action="store_true",dest="no_metadata",
                      default=False)
    parser.add_option("-b","--bitrate",
                      action="store_true",dest="show_bitrate")

    (options,args) = parser.parse_args()

    show_metadata = not options.no_metadata
    show_bitrate = options.show_bitrate

    for file in audiotools.open_files(args):
        length = file.cd_frames() / 75
        if (not show_bitrate):
            print (u"%(minutes)2.2d:%(seconds)2.2d %(channels)dch %(rate)dHz %(bits)d-bit: %(filename)s" % \
                       {"minutes":length / 60,
                        "seconds":length % 60,
                        "channels":file.channels(),
                        "rate":file.sample_rate(),
                        "bits":file.bits_per_sample(),
                        "filename":file.filename.decode(audiotools.FS_ENCODING,'replace')}).encode(audiotools.IO_ENCODING,'replace')
        else:
            print (u"%(bitrate)4.4s kbps: %(filename)s" % \
                       {'bitrate':
                            ((os.path.getsize(file.filename) * 8) / 2 ** 10) / \
                            length,
                        'filename':file.filename.decode(audiotools.FS_ENCODING,'replace')}).encode(audiotools.IO_ENCODING,'replace')
        if (show_metadata):
            metadata = file.get_metadata()
            if (metadata != None):
                print unicode(metadata).encode(audiotools.IO_ENCODING,'replace')
                print

